package com.fingard.dsp.bank.directbank.shie01;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;
import com.shie.openapi.dto.ResponseDto;

public class SHIE01SinglePay extends SHIE01Base implements ISinglePay {
	/**
	 * 1、单笔对外支付          190802
	 * 2、一户通账户充值     190812
	 */
	@Override
	public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
		
		RetAtsSglDetail tmpRetDetail = null;
		
		String commandCode = p_req1908.reqHeader.commandCode;
		// 单笔支付 一户通
		if ("190802".equals(commandCode)) {
			tmpRetDetail = d_190802(p_req1908);
		}
		// 一户通充值
		if ("190812".equals(commandCode)) {
			tmpRetDetail = d_190812(p_req1908);
		}
		/*单笔支付*/
		if ("190803".equals(commandCode)) {
			tmpRetDetail = d_190803(p_req1908);
		}


		if (StringHelper.isNullOrEmpty(tmpRetDetail.reqSeqID)) {
			tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;
		}

		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}

	@Override
	public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
		return null;
	}


	/**
	 *  190803单笔支付
	 *
	 * @param p_req1908
	 * @return
	 * @throws Exception
	 */
	private RetAtsSglDetail d_190803(ReqSingle p_req1908) throws Exception {
		RetAtsSglDetail retDetail = new RetAtsSglDetail();
		retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
		ActSetItem actItem = getBankConfig().getActSetItem(p_req1908.singleDetail.payAct, "*");
		if (actItem == null || !FileHelper.existFile(actItem.ownKeyStorePath)) {
			retDetail.respCode = "0";
			if (actItem == null) {
				retDetail.respInfo = "未找到账号" + p_req1908.singleDetail.payAct + "的商户配置节点";
			} else {
				retDetail.respInfo = "账号" + p_req1908.singleDetail.payAct + "的商户证书文件不存在";
			}
			retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
			retDetail.transAmt = p_req1908.singleDetail.payAmount;
			retDetail.transState = PaymentState.FAIL_Payment;
			retDetail.payInfoCode = FailCode.E9981;
			return retDetail;
		}
		String serviceCode = SHIE01TransType.P190803;

		Map<String, Object> bizContent = new HashMap<String, Object>();
		/* 商户代码*/
		bizContent.put("comCode", actItem.inMerchantId);
		/* 订单号*/
		bizContent.put("orderId", p_req1908.singleDetail.askNumber);
		/* 付款银行账户*/
		bizContent.put("payerAcctNo", p_req1908.singleDetail.payAct);
		/* 付款银行户名*/
		bizContent.put("payerAcctName", p_req1908.singleDetail.payName);
		/* 收款银行行别  参考附件《保交所银行行别编码》*/
		bizContent.put("bankType", p_req1908.singleDetail.recBankCode);
		/*收款银行支行名称*/
		bizContent.put("bankName", p_req1908.singleDetail.recBankName + p_req1908.singleDetail.recBankLocation);
		/*收款银行支行号*/
		bizContent.put("bankBranchNo", p_req1908.singleDetail.cnapsCode);
		/*收款方户名*/
		bizContent.put("bankCardName", p_req1908.singleDetail.recName);
		/* 收款银行卡号*/
		bizContent.put("bankCardNo", p_req1908.singleDetail.recAct);
		/* 收款方账号类型  0(卡) 1（存折）*/
		String cardtype = "0";
		if ("1".equals(p_req1908.singleDetail.cardType)) {
			cardtype = "1";
		}
		bizContent.put("accountType", cardtype);
		/* 交易币种*/
		bizContent.put("currenoy", "CNY");/* CNY： 人民币*/
		/* 对公账号|对私账号 类型*/
		if ("1".equals(p_req1908.singleDetail.isPrivate)) {
			bizContent.put("pubPriv", "2");/* 2: 对私*/
		} else {
			bizContent.put("pubPriv", "1");
		}
		/* 金额  单位为分*/
		bizContent.put("amount", StringHelper.amountMultiply100(p_req1908.singleDetail.payAmount));
		/* 异步通知地址*/
		bizContent.put("notifyURL", actItem.notifyURL);
		ResponseDto responseDto = sendToSHIE(actItem, serviceCode, bizContent);

		/* 解析银行返回*/
		String tmpRespCode = responseDto.getResponseCode();
		if ("0000".equals(tmpRespCode)) {
			JSONObject respBizContent = JSON.parseObject(responseDto.getBizContent());
			String msgCode = respBizContent.getString("msgCode");
			retDetail.respCode = "0";
			if ("0000".equals(msgCode)) {
				retDetail.respCode = "0";
				retDetail.respInfo = respBizContent.getString("payResultMsg");
				retDetail.reqSeqID = respBizContent.getString("orderId");
				retDetail.transAmt = StringHelper.amountDivide100((String)respBizContent.getString("amount"));
				retDetail.transState = tmpRespCode + "+" + msgCode + "+" + respBizContent.get("payResult");
				retDetail.payInfoCode = respBizContent.getString("payResultMsg");
			} else {
				retDetail.transState = msgCode;
				retDetail.payInfoCode = msgCode;
				retDetail.respInfo = respBizContent.getString("msgInfo");
			}

		} else {
			retDetail.respCode = "0";
			retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
			retDetail.transAmt = p_req1908.singleDetail.payAmount;
			retDetail.transState = tmpRespCode;
			retDetail.payInfoCode = tmpRespCode;
			retDetail.respInfo = responseDto.getResponseDesc();
		}

		return retDetail;
	}



	/**
	 *  190802单笔对外支付  一户通
	 *
	 * @param p_req1908
	 * @return
	 * @throws Exception
	 */
	private RetAtsSglDetail d_190802(ReqSingle p_req1908) throws Exception {
		RetAtsSglDetail retDetail = new RetAtsSglDetail();

		ActSetItem actItem = getBankConfig().getActSetItem(p_req1908.singleDetail.payAct, "*");
		if (actItem == null || !FileHelper.existFile(actItem.ownKeyStorePath)) {
			retDetail.respCode = "0";
			if (actItem == null) {
				retDetail.respInfo = "未找到账号" + p_req1908.singleDetail.payAct + "的商户配置节点";
			} else {
				retDetail.respInfo = "账号" + p_req1908.singleDetail.payAct + "的商户证书文件不存在";
			}

			retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
			retDetail.transAmt = p_req1908.singleDetail.payAmount;
			retDetail.transState = PaymentState.FAIL_Payment;
			retDetail.payInfoCode = FailCode.E9981;

			return retDetail;
		}

		String serviceCode = SHIE01TransType.P190802;


		Map<String, Object> bizContent = new HashMap<String, Object>();

		// 商户代码
		bizContent.put("comCode", actItem.inMerchantId);
		// 一户通账号
		bizContent.put("payeeCustId", actItem.virtualAct);
		// 订单号
		bizContent.put("orderId", p_req1908.singleDetail.askNumber);
		// 商品代码
		bizContent.put("productCode", getProductCode());
		// 商户渠道ID
		bizContent.put("accChanId", actItem.merchantId);
		// 收款银行行别
		bizContent.put("bankType", p_req1908.singleDetail.recBankCode);
		// 收款银行支行名称
		bizContent.put("bankName", p_req1908.singleDetail.recBankName + p_req1908.singleDetail.recBankLocation);
		// 收款方户名
		bizContent.put("bankCardName", p_req1908.singleDetail.recName);
		// 收款银行卡号
		bizContent.put("bankCardNo", p_req1908.singleDetail.recAct);
		// 交易币种
		bizContent.put("currenoy", "1");// 1 ： 人民币
		// 对公账号|对私账号 类型
		if ("1".equals(p_req1908.singleDetail.isPrivate)) {
			bizContent.put("pubPriv", "2");// 2: 对私
		} else {
			bizContent.put("pubPriv", "1");
		}

		// 金额  单位为分
		bizContent.put("amount", StringHelper.amountMultiply100(p_req1908.singleDetail.payAmount));
		// 异步通知地址
		bizContent.put("notifyURL", actItem.notifyURL);
		
		ResponseDto responseDto = sendToSHIE(actItem, serviceCode, bizContent);
		
		// 解析银行返回
		String tmpRespCode = responseDto.getResponseCode();
		if ("0000".equals(tmpRespCode)) {
			JSONObject respBizContent = JSON.parseObject(responseDto.getBizContent());
			String msgCode = respBizContent.getString("msgCode");
			if ("0000".equals(msgCode)) {
				retDetail.respCode = "0";
				retDetail.reqSeqID = respBizContent.getString("orderId");
				retDetail.transAmt = StringHelper.amountDivide100((String)respBizContent.getString("amount"));
				retDetail.transState = tmpRespCode + "+" + msgCode + "+" + respBizContent.get("payResult");
				retDetail.payInfoCode = respBizContent.getString("payResultMsg");
			} else {
				retDetail.respCode = "-1";
			}
			retDetail.respInfo = respBizContent.getString("msgInfo");
		} else {
			if (!"9105".equals(tmpRespCode)) {
				retDetail.respCode = "-1";
			} else {
				retDetail.respCode = "0";
				retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
				retDetail.transAmt = p_req1908.singleDetail.payAmount;
				retDetail.transState = PaymentState.FAIL_Payment;
				retDetail.payInfoCode = tmpRespCode;
			}

			retDetail.respInfo = responseDto.getResponseDesc();
		}

		return retDetail;
	}

	/**
	 * 一户通账户充值
	 * 
	 * @param p_req1908
	 * @return
	 * @throws Exception
	 */
	private RetAtsSglDetail d_190812(ReqSingle p_req1908) throws Exception {
		RetAtsSglDetail retDetail = new RetAtsSglDetail();

		ActSetItem actItem = getBankConfig().getActSetItem(p_req1908.singleDetail.recAct, "*");
		if (actItem == null || !FileHelper.existFile(actItem.ownKeyStorePath)) {
			retDetail.respCode = "0";
			if (actItem == null) {
				retDetail.respInfo = "未找到账号" + p_req1908.singleDetail.recAct + "的商户配置节点";
			} else {
				retDetail.respInfo = "账号" + p_req1908.singleDetail.recAct + "的商户证书文件不存在";
			}
			
			retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
			retDetail.transAmt = p_req1908.singleDetail.payAmount;
			retDetail.transState = PaymentState.FAIL_Payment;
			retDetail.payInfoCode = FailCode.E9981;
			
			return retDetail;
		}

		String serviceCode = SHIE01TransType.P190812;

		Map<String, Object> bizContent = new HashMap<String, Object>();
		
		// 商户代码
		bizContent.put("comCode", actItem.inMerchantId);
		// 充值流水号
		bizContent.put("rechargeNo", p_req1908.singleDetail.askNumber);
		// 一户通账号
		bizContent.put("custId", p_req1908.singleDetail.recAct);
		// 付款方账号
		bizContent.put("payerAccNo", p_req1908.singleDetail.payAct);
		// 付款方户名
		bizContent.put("payerAccName", p_req1908.singleDetail.payName);
		// 账户业务类型
		bizContent.put("accountType", getBankConfig().getMapValueByKey("accountType", "1"));
		// 账户归属
		bizContent.put("accountOwner", "04");
		// 金额  单位为分
		bizContent.put("amount", StringHelper.amountMultiply100(p_req1908.singleDetail.payAmount));
		
		ResponseDto responseDto = sendToSHIE(actItem, serviceCode, bizContent);
		
		// 解析银行返回
		String tmpRespCode = responseDto.getResponseCode();
		if ("0000".equals(tmpRespCode)) {
			JSONObject respBizContent = JSON.parseObject(responseDto.getBizContent());
			
			String msgCode = respBizContent.getString("msgCode");
			if ("0000".equals(msgCode)) {
				retDetail.respCode = "0";
				retDetail.reqSeqID = respBizContent.getString("rechargeNo");
				retDetail.transAmt = p_req1908.singleDetail.payAmount;
				retDetail.transState = tmpRespCode + "+" + msgCode + "+" + respBizContent.get("rechargeResult");
				retDetail.payInfoCode = respBizContent.getString("rechargeResultMsg");
			} else {
				retDetail.respCode = "-1";
			}
			
			retDetail.respInfo = respBizContent.getString("msgInfo");
		} else {
			if ("9105".equals(tmpRespCode)) {
				retDetail.respCode = "0";
				retDetail.transAmt = p_req1908.singleDetail.payAmount;
				retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
				retDetail.transState = PaymentState.FAIL_Payment;
				retDetail.payInfoCode = tmpRespCode;
			} else {
				retDetail.respCode = "-1";
			}
			
			retDetail.respInfo = responseDto.getResponseDesc();
		}
		
		return retDetail;
	}

	@Override
	public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {

		if ("190803".equals(p_req1944.reqHeader.commandCode)) {
			return d_190803Qry(p_req1944);
		} else {
			return d_190802(p_req1944);
		}

	}

	public RetAtsSingle d_190802(ReqQrySingle p_req1944) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
		RetAtsSglDetail tmpRetAtsDetail = new RetAtsSglDetail();

		try {
			ActSetItem actItem = getBankConfig().getActSetItem(p_req1944.payAct, "*");
			if (actItem == null || !FileHelper.existFile(actItem.ownKeyStorePath)) {
				tmpRetAtsDetail.respCode = "-1";
				if (actItem == null) {
					tmpRetAtsDetail.respInfo = "未找到账号[" + p_req1944.payAct + "]商户配置节点";
				} else {
					tmpRetAtsDetail.respInfo = "账号[" + p_req1944.payAct + "]证书文件不存在";
				}

				retAts.writeRetDetail(tmpRetAtsDetail);
				return retAts;
			}

			String serviceCode = SHIE01TransType.Q190802;

			Map<String, Object> bizContent = new HashMap<String, Object>();

			// 机构订单号
			bizContent.put("orderId", p_req1944.askNumber);
			
			ResponseDto responseDto = sendToSHIE(actItem, serviceCode, bizContent);

			// 解析银行返回
			String tmpRespCode = responseDto.getResponseCode();
			if ("0000".equals(tmpRespCode)) {
				JSONObject respBizContent = JSON.parseObject(responseDto.getBizContent());

				String msgCode = respBizContent.getString("msgCode");
				if ("0000".equals(msgCode)) {
					tmpRetAtsDetail.respCode = "0";
					tmpRetAtsDetail.reqSeqID = respBizContent.getString("orderId");
					tmpRetAtsDetail.transAmt = StringHelper.amountDivide100((String)respBizContent.getString("amount"));
					tmpRetAtsDetail.transState = tmpRespCode + "+" + msgCode + "+" + respBizContent.getString("payResult");
					tmpRetAtsDetail.payInfoCode = respBizContent.getString("payResultMsg");
				} else {
					tmpRetAtsDetail.respCode = "-1";
				}

				tmpRetAtsDetail.respInfo = respBizContent.getString("payResultMsg");
			} else {
				tmpRetAtsDetail.respCode = "-1";
				tmpRetAtsDetail.respInfo = responseDto.getResponseDesc();
			}
		} catch (Exception e) {
			tmpRetAtsDetail.respCode = "-1";
			tmpRetAtsDetail.respInfo = e.getMessage();

			e.printStackTrace();
		}

		if (StringHelper.isNullOrEmpty(tmpRetAtsDetail.reqSeqID)) {
			tmpRetAtsDetail.reqSeqID = p_req1944.askNumber;
		}

		retAts.writeRetDetail(tmpRetAtsDetail);
		return retAts;
	}

	public RetAtsSingle d_190803Qry(ReqQrySingle p_req1944) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
		RetAtsSglDetail tmpRetAtsDetail = new RetAtsSglDetail();
		try {
			ActSetItem actItem = getBankConfig().getActSetItem(p_req1944.payAct, "*");
			if (actItem == null || !FileHelper.existFile(actItem.ownKeyStorePath)) {
				tmpRetAtsDetail.respCode = "-1";
				if (actItem == null) {
					tmpRetAtsDetail.respInfo = "未找到账号[" + p_req1944.payAct + "]商户配置节点";
				} else {
					tmpRetAtsDetail.respInfo = "账号[" + p_req1944.payAct + "]证书文件不存在";
				}

				retAts.writeRetDetail(tmpRetAtsDetail);
				return retAts;
			}

			String serviceCode = SHIE01TransType.Q190803;
			Map<String, Object> bizContent = new HashMap<String, Object>();
			/*商户代码*/
			bizContent.put("comCode", actItem.inMerchantId);
			/*机构订单号*/
			bizContent.put("orderId", p_req1944.askNumber);
			/*查询页数*/
			bizContent.put("pageNo", "1");
			ResponseDto responseDto = sendToSHIE(actItem, serviceCode, bizContent);
			// 解析银行返回
			String tmpRespCode = responseDto.getResponseCode();
			if ("0000".equals(tmpRespCode)) {
				JSONObject respBizContent = JSON.parseObject(responseDto.getBizContent());
				String msgCode = respBizContent.getString("msgCode");
				if ("0000".equals(msgCode)) {
					JSONArray jsonArray = respBizContent.getJSONArray("details");
					/*JSONArray respBodyJson = JSONArray.parseArray(respBizContent.getString("details"));*/
					JSONObject ojsonArray = JSONObject.parseObject(jsonArray.get(0).toString());
					tmpRetAtsDetail.respCode = "0";
					tmpRetAtsDetail.respCode = ojsonArray.getString("payResultMsg");
					tmpRetAtsDetail.reqSeqID = ojsonArray.getString("orderId");
					String amt = ojsonArray.getString("amount");
					if (amt != null && !"".equals(amt)) {
						tmpRetAtsDetail.transAmt = StringHelper.amountDivide100((amt) );
					}
					tmpRetAtsDetail.transState = tmpRespCode + "+" + msgCode + "+" + ojsonArray.getString("payResult");
					tmpRetAtsDetail.payInfoCode = ojsonArray.getString("payResultMsg");

				} else {
					tmpRetAtsDetail.respCode = "-1";
				}

				tmpRetAtsDetail.respInfo = respBizContent.getString("payResultMsg");
			} else {
				tmpRetAtsDetail.respCode = "-1";
				tmpRetAtsDetail.respInfo = responseDto.getResponseDesc();
			}
		} catch (Exception e) {
			tmpRetAtsDetail.respCode = "-1";
			tmpRetAtsDetail.respInfo = e.getMessage();
			e.printStackTrace();
		}
		if (StringHelper.isNullOrEmpty(tmpRetAtsDetail.reqSeqID)) {
			tmpRetAtsDetail.reqSeqID = p_req1944.askNumber;
		}
		retAts.writeRetDetail(tmpRetAtsDetail);
		return retAts;
	}

	@Override
	public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
		return null;
	}
}
